package com.example.wechatlistview.controls;

import android.content.Context;
import android.widget.SimpleAdapter;

import androidx.annotation.LayoutRes;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 万能适配器类
 *
 * @author 陈炜龙
 */
public class SimperListAdapter {

    private Context context;
    private int layoutId;
    private String[] itemName;
    private Object[][] ResArray;
    private int[] controls;

    private List<Map<String, Object>> list;

    /**
     * 万能适配器构造方法
     *
     * @param context  上下文
     * @param id       项目布局id
     * @param itemName 键数组
     * @param ResArray 资源组集合
     * @param controls 控件id数组
     */
    public SimperListAdapter(Context context, @LayoutRes int id, String[] itemName, Object[][] ResArray, int[] controls) {
        this.context = context;
        this.layoutId = id;
        this.itemName = itemName;
        this.ResArray = ResArray;
        this.controls = controls;
        list = new ArrayList<Map<String, Object>>();

    }


    /**
     * 自动绑定资源集与键值
     *
     * @return 绑定后的列表List
     */
    public List<Map<String, Object>> GetProcessedList() {
        for (int i = 0; i < ResArray[0].length; i++) {
            Map<String, Object> map = new HashMap<String, Object>(ResArray[0].length);
            //循环读取资源数组，并赋予键值
            for (int j = 0; j < itemName.length; j++) {
                map.put(itemName[j], ResArray[j][i]);
            }
            list.add(map);
        }
        return list;
    }

    /**
     * 处理并绑定列表后分配至适配器
     *
     * @return 处理后的简单适配器
     */
    public SimpleAdapter GetAdapter() {
        //for循环将内容资源（图像资源id,文字字符串）包装在Map中，再统一分配在List集合
        List<Map<String, Object>> list = GetProcessedList();

        //处理List并分配至简单适配器
        SimpleAdapter simpleAdapter = new SimpleAdapter(context, list, layoutId, itemName, controls);
        return simpleAdapter;


    }

    /**
     * 静态类
     * 快速获取适配器
     */
    public static SimpleAdapter get(Context context, @LayoutRes int id, String[] name, Object[][] resArray, int[] ctrl) {
        List<Map<String, Object>> FastList = new ArrayList<Map<String, Object>>();
        //for循环将内容资源（图像资源id,文字字符串）包装在Map中，再统一分配在List集合
        FastList = SimperListAdapter.GetProcessedList(name, resArray);

        //处理List并分配至简单适配器
        SimpleAdapter simpleAdapter = new SimpleAdapter(context, FastList, id, name, ctrl);
        return simpleAdapter;


    }

    /**
     * 静态类
     * 自动绑定资源集与键值
     *
     * @param itemName 键值数组
     * @param ResArray 资源组集合（二维数组）
     * @return 绑定后的列表List
     */
    public static List<Map<String, Object>> GetProcessedList(String[] itemName, Object[][] ResArray) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < ResArray[0].length; i++) {
            Map<String, Object> map = new HashMap<String, Object>(ResArray[0].length);
            //循环读取资源数组，并赋予键值
            for (int j = 0; j < itemName.length; j++) {
                map.put(itemName[j], ResArray[j][i]);
            }
            list.add(map);
        }
        return list;
    }


}
